package com.miyake.loveviewweb.client.plugin;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.shared.PluginType;
import com.miyake.loveviewweb.shared.PluginTypeElement;
import com.miyake.loveviewweb.shared.model.PluginModel;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;

public class PluginMarket extends Composite {
    public interface PluginSearchHandler {
        void onInstalled();
    };

    private Button buttonInstall;
    private VerticalPanel panel;

    private PluginListDataGrid pluginList;
    private PluginSearchHandler handler;
    

    public PluginMarket() {
        initialize("*");
    }
    
    public PluginMarket(String filter) {
        initialize(filter);
    }
    

    private void initialize(String filter) {
        if (filter == null) {
            filter = "*";
        }
        panel = new VerticalPanel();
        initWidget(panel);
        panel.clear();
        pluginList = new PluginListDataGrid();
        
        HorizontalPanel toolBar = new HorizontalPanel();
        panel.add(toolBar);
        final ListBox typeList = new ListBox();
        typeList.addItem("*", "*");
        for (PluginTypeElement e : PluginType.getInstance().getPlugins()) {
            typeList.addItem(e.getCaption(), e.getId());
            if (filter.equals(e.getId())) {
                typeList.setSelectedIndex(typeList.getItemCount()-1);
            }
        }
        typeList.addChangeHandler(new ChangeHandler() {
            public void onChange(ChangeEvent event) {
                updatePluginList(typeList.getValue(typeList.getSelectedIndex()), "");  
            }
        });
        toolBar.add(typeList);
        
        
        toolBar.add(new HTML("Keywords"));
        TextBox textBox = new TextBox();
        textBox.setWidth("200px");
        toolBar.add(textBox);
        panel.add(pluginList);
        buttonInstall = new Button("Install");
        panel.add(buttonInstall);
        String category = "";
        String tag = "";
//        updatePluginList(category, tag);  
        
        buttonInstall.addClickHandler(new ClickHandler() {

            public void onClick(ClickEvent event) {
                onButtonInstallClick(event);
            }
            
        });
        
        updatePluginList(typeList.getItemText(typeList.getSelectedIndex()), "");  
    }

    private void updatePluginList(String category, String tag) {
        pluginList.clear();
        DataService.Util.getInstance().getPluginList(category, tag, new AsyncCallback<List<PluginModel>>() {

            public void onFailure(Throwable caught) {

            }

            public void onSuccess(List<PluginModel> result) {
                pluginList.addData(PluginModelEx.getPluginModelEx(result));
            }
            
        });
    }

    void onButtonUninstallClick(ClickEvent event) {
        List<PluginModelEx> data = pluginList.getData();
        List<Key> keys = new ArrayList<Key>();
        for (PluginModelEx p : data) {
            if (p.isSelected()) {
                keys.add(p.getPluginModel().getKey());
            }
        }
       
        DataService.Util.getInstance().uninstallPlugins(keys, new AsyncCallback<Void>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(Void result) {
                if (handler != null) {
                    handler.onInstalled();
                }
            }
            
        });
    }

    void onButtonInstallClick(ClickEvent event) {
        
        DataService.Util.getInstance().installPlugins(pluginList.getSelectedKeys(), new AsyncCallback<Void>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(Void result) {
                // TODO Auto-generated method stub
                
            }
            
        });
    }

    /**
     * @return the handler
     */
    public PluginSearchHandler getHandler() {
        return handler;
    }

    /**
     * @param handler the handler to set
     */
    public void setHandler(PluginSearchHandler handler) {
        this.handler = handler;
    }

    public void update() {
        initialize("*");
    }
}
